/*
Released under this MIT License:
	
Copyright (c) 2010 Bart van der Schoor
	
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
	
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
	
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
	
 */
package bruce 
{
	import flash.display.DisplayObject;
	import flash.net.URLVariables;
	/**
	 *
	 *	URLVars
	 *
	 *	 do stuff with urls and vars (loaderInfo.parameters, or other generic Object's), like URLVars.addRootVars();
	 *	 
	 *	 part of the Bruce Simple AS3 Banner Toolkit
	 *	 
	 * @author Bart van der Schoor @ GML
	 * @version 0.6
	 */
	public class URLVars 
	{
		private static const TYPE:String = 'URLVars.';

		public static function addRootVars(clip:DisplayObject, url:String, overwrite:Boolean = true, extra:Object = null):String
		{
			if (!url)
			{
				Bruce.arg(TYPE + 'addRootVars() url' + Bruce.eEmpty);
				return '';
			}
			if (!clip)
			{
				Bruce.arg(TYPE + 'addRootVars()' + Bruce.eclNull);
				return url;
			}
			if (!clip.root)
			{
				Bruce.arg(TYPE + 'addRootVars() clip is not on the displayList');
				return url;
			}
			
			url = URLVars.addToURL(url, clip.root.loaderInfo.parameters, overwrite);
			
			var stageVars:Object;			
			try
			{
				if(clip.stage)
				{
					stageVars = clip.stage.loaderInfo.parameters;
				}
			}
			catch(error:Error)
			{
				//SecurityError probably
				Bruce.log(TYPE + 'grab() supressed an error while scanning stage: ' + error);
				//Log.arg(TYPE+'grab() with tryStage=true while your clip cannot access the stage: is it actually on it? or do we have a SecurityError?\n\n'+error);
			}
			
			if(stageVars)
			{				url = URLVars.addToURL(url, stageVars, overwrite);
			}
			if(extra)
			{				url = URLVars.addToURL(url, extra, overwrite);
			}
			return url;
		}

		public static function addToURL(url:String, vars:Object, overwrite:Boolean = true):String
		{
			if (!url)
			{
				Bruce.arg(TYPE + 'addToURL() url' + Bruce.eEmpty);
				return '';
			}
			if (!vars)
			{
				Bruce.arg(TYPE + 'addToURL() vars' + Bruce.eNull);
				return url;
			}
			
			var hash:URLVariables = new URLVariables();		
			
			var query:String = URLVars.grab(url);
			if(query)
			{
				hash.decode(query);
			}
			
			for(var name:String in vars)
			{
				if(overwrite || !(name in hash))
				{
					hash[name] = vars[name];
				}
			}
			
			query = hash.toString();
			if(query.length == 0)
			{
				//all this for.. nothing?
				return url;
			}
			//good
			return URLVars.strip(url) + '?' + query;
		}

		public static function strip(url:String):String
		{
			if (!url)
			{
				Bruce.arg(TYPE + 'strip() url' + Bruce.eEmpty);
				return '';
			}
			if(url.indexOf('?') > 0)
			{
				//chop query-string
				return url.substring(0, url.indexOf('?'));
			}
			return url;
		}

		public static function grab(url:String):String
		{
			if (!url)
			{
				Bruce.arg(TYPE + 'grab() url' + Bruce.eEmpty);
				return '';
			}
			if(url.indexOf('?') > 0)
			{
				//chop query-string
				return url.substring(url.indexOf('?') + 1);
			}
			return '';
		}

		public static function vars(url:String):URLVariables
		{
			if (!url)
			{
				Bruce.arg(TYPE + 'grab() url' + Bruce.eEmpty);
				return null;
			}
			if(url.indexOf('?') > 0)
			{
				//chop query-string
				return new URLVariables(url.substring(url.indexOf('?') + 1));
			}
			return new URLVariables();
		}
	}
}
